package com.kwedding.service.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kwedding.bean.TB_Account;
import com.kwedding.dao.TB_AccountMapper;
import com.kwedding.dto.app.RequestLogin;
import com.kwedding.dto.app.ResponseAccountInfo;
import com.kwedding.util.CheckedUtil;
import com.kwedding.util.Constants;
import com.kwedding.util.CopyerObject;
import com.kwedding.util.LoggerParent;
import com.kwedding.util.RequestErrorBackException;

@Service
public class AccountService extends LoggerParent {

	@Autowired
	private TB_AccountMapper tb_AccountMapper;

	/**
	 * 平板用户登录
	 * 
	 * @param login
	 */
	@Transactional(readOnly = true)
	public ResponseAccountInfo login(RequestLogin login) {
		CheckedUtil.isNull(login.getUserName(), Constants.REQUEST_ERROR_1, "用户名不能为空");
		CheckedUtil.isNull(login.getPassword(), Constants.REQUEST_ERROR_2, "密码不能为空");
		TB_Account tb_Account = tb_AccountMapper.login(login.getUserName());
		if (tb_Account == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_3, "账号不存在");
		}
		if (!tb_Account.getPassword().equals(login.getPassword())) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_4, "密码错误，请核对用户名密码");
		}
		ResponseAccountInfo accountInfo = CopyerObject.copyerObject(tb_Account, ResponseAccountInfo.class);
		return accountInfo;
	}
}
